package com.hdax.dm.service;

import com.hdax.dm.common.exception.user.UserLoginException;
import com.hdax.dm.entity.base.Image;
import com.hdax.dm.entity.item.Item;
import com.hdax.dm.entity.item.ItemComment;
import com.hdax.dm.feign.base.ImageFeign;
import com.hdax.dm.feign.item.ItemCommentFeign;
import com.hdax.dm.feign.item.ItemFeign;
import com.hdax.dm.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ItemCService {

    @Autowired
    private ItemFeign itemFeign;
    @Autowired
    private ImageFeign imageFeign;
    @Autowired
    private ItemCommentFeign itemCommentFeign;

    public List<RecommendVo> recommend(){
        List<RecommendVo> vos = new ArrayList<>();
        List<Item> lists = itemFeign.recommend();
        lists.forEach(list->{
            RecommendVo vo = new RecommendVo();
            BeanUtils.copyProperties(list,vo);
            Image image = imageFeign.image(list.getId(), 2L, 1L);
            if (image != null){
                vo.setImgUrl(image.getImgUrl()==""?"":image.getImgUrl());
            }
            vos.add(vo);
        });
        return vos;
    }

    public List<RecommendVo> sell(){
        List<RecommendVo> vos = new ArrayList<>();
        List<Item> lists = itemFeign.sell();
        lists.forEach(list->{
            RecommendVo vo = new RecommendVo();
            BeanUtils.copyProperties(list,vo);
            Image image = imageFeign.image(list.getId(), 2L, 1L);
            if (image != null){
                vo.setImgUrl(image.getImgUrl()==""?"":image.getImgUrl());
            }
            vos.add(vo);
        });
        return vos;
    }

    public List<seniorityVo> seniority(Long itemTypeId){
        List<seniorityVo> vos = new ArrayList<>();
        List<Item> lists = itemFeign.seniority(itemTypeId);
        lists.forEach(list->{
            seniorityVo vo = new seniorityVo();
            BeanUtils.copyProperties(list,vo);
            vo.setAreaId(list.getCinema().getAreaId());
            vo.setAreaName(list.getCinema().getAreaName());
            vos.add(vo);
        });
        return vos;
    }

    public List<guesslikeVo> guesslike(Long itemTypeId){
        List<guesslikeVo> vos = new ArrayList<>();
        List<Item> lists = itemFeign.guesslike(itemTypeId);
        lists.forEach(list->{
            guesslikeVo vo = new guesslikeVo();
            vo.setAreaId(list.getCinema().getAreaId());
            vo.setAreaName(list.getCinema().getAreaName());
            vo.setAddress(list.getCinema().getAddress());
            vo.setStartDate(list.getStartTime());
            vo.setEndDate(list.getEndTime());
            BeanUtils.copyProperties(list,vo);
            Image image = imageFeign.image(list.getId(), 2L, 1L);
            if (image != null){
                vo.setImgUrl(image.getImgUrl()==""?"":image.getImgUrl());
            }
            vos.add(vo);
        });
        return vos;
    }

    public List<getCommentsVo> getComments(Long id){
        List<getCommentsVo> vos = new ArrayList<>();
        List<ItemComment> lists = itemCommentFeign.getComments(id);
        lists.forEach(list->{
            getCommentsVo vo = new getCommentsVo();
            BeanUtils.copyProperties(list,vo);
            Image image = imageFeign.image(list.getUserId(), 0L, 0L);
            if (image != null){
                vo.setImgUrl(image.getImgUrl()==""?"":image.getImgUrl());
            }
            vos.add(vo);
        });
        return vos;
    }

    public List<getCommentsVo> addComments(Map<String,String> map){
        //添加
        Long id = Long.parseLong(map.get("itemId"));
        Long userId = Long.parseLong(map.get("userId"));
        String comment = map.get("comment");
        Double score = Double.parseDouble(map.get("score").trim());
        ItemComment itemComment = new ItemComment();
        itemComment.setId(null);
        itemComment.setItemId(id);
        itemComment.setUserId(userId);
        itemComment.setScore(score);
        itemComment.setContent(comment);
        itemComment.setCreatedTime(LocalDateTime.now());
        itemComment.setUpdatedTime(null);
        boolean b = itemCommentFeign.addComments(itemComment);

        List<getCommentsVo> vos = new ArrayList<>();
        List<ItemComment> lists = itemCommentFeign.getComments(id);
        lists.forEach(list->{
            getCommentsVo vo = new getCommentsVo();
            BeanUtils.copyProperties(list,vo);
            Image image = imageFeign.image(list.getUserId(), 0L, 0L);
            if (image != null){
                vo.setImgUrl(image.getImgUrl()==""?"":image.getImgUrl());
            }
            vos.add(vo);
        });
        return vos;
    }

    public List<getRecommendVo> getRecommend(Long itemTypeId){
        List<getRecommendVo> vos = new ArrayList<>();
        List<Item> lists = itemFeign.getRecommend(itemTypeId);
        lists.forEach(list->{
            getRecommendVo vo = new getRecommendVo();
            BeanUtils.copyProperties(list,vo);
            vo.setAreaId(list.getCinema().getAreaId());
            vo.setAreaName(list.getCinema().getAreaName());
            vo.setAddress(list.getCinema().getAddress());
            vo.setStartDate(list.getStartTime());
            vo.setEndDate(list.getEndTime());
            Image image = imageFeign.image(list.getId(),2L, 1L);
            if (image != null){
                vo.setImgUrl(image.getImgUrl()==""?"":image.getImgUrl());
            }
            vos.add(vo);
        });
        return vos;
    }

    public List<CarouselDto> getCarouselData(Long itemTypeId){
        List<CarouselDto> vos = new ArrayList();
        List<Item> lists = itemFeign.getCarouselData(itemTypeId);
        lists.forEach(list->{
            CarouselDto vo = new CarouselDto();
            BeanUtils.copyProperties(list,vo);
            vo.setImgUrl(list.getImage().getImgUrl());
            vos.add(vo);
        });
        return vos;
    }

    public List<getRecommendVo> getAdimgUrl(Long itemTypeId){
        List<getRecommendVo> vos = new ArrayList<>();
        List<Item> lists = itemFeign.getAdimgUrl(itemTypeId);
        lists.forEach(list->{
            getRecommendVo vo = new getRecommendVo();
            BeanUtils.copyProperties(list,vo);
            vo.setAreaId(list.getCinema().getAreaId());
            vo.setAreaName(list.getCinema().getAreaName());
            vo.setAddress(list.getCinema().getAddress());
            vo.setStartDate(list.getStartTime());
            vo.setEndDate(list.getEndTime());
            Image image = imageFeign.image(list.getId(),0L, 1L);
            if (image != null){
                vo.setImgUrl(image.getImgUrl()==""?"":image.getImgUrl());
            }
            vos.add(vo);
        });
        return vos;
    }

    public List<getRecommendVo> getWonderfulData(Long itemTypeId){
        List<getRecommendVo> vos = new ArrayList<>();
        List<Item> lists = itemFeign.getWonderfulData(itemTypeId);
        lists.forEach(list->{
            getRecommendVo vo = new getRecommendVo();
            BeanUtils.copyProperties(list,vo);
            vo.setAreaId(list.getCinema().getAreaId());
            vo.setAreaName(list.getCinema().getAreaName());
            vo.setAddress(list.getCinema().getAddress());
            vo.setStartDate(list.getStartTime());
            vo.setEndDate(list.getEndTime());
            Image image = imageFeign.image(list.getId(),2L, 1L);
            if (image != null){
                vo.setImgUrl(image.getImgUrl()==""?"":image.getImgUrl());
            }
            vos.add(vo);
        });
        return vos;
    }

    public List<getRecommendVo> getPopularSearch(Long itemTypeId){
        List<getRecommendVo> vos = new ArrayList<>();
        List<Item> lists = itemFeign.getPopularSearch(itemTypeId);
        lists.forEach(list->{
            getRecommendVo vo = new getRecommendVo();
            BeanUtils.copyProperties(list,vo);
            vo.setAreaId(list.getCinema().getAreaId());
            vo.setAreaName(list.getCinema().getAreaName());
            vo.setAddress(list.getCinema().getAddress());
            vo.setStartDate(list.getStartTime());
            vo.setEndDate(list.getEndTime());
            Image image = imageFeign.image(list.getId(),2L, 1L);
            if (image != null){
                vo.setImgUrl(image.getImgUrl()==""?"":image.getImgUrl());
            }
            vos.add(vo);
        });
        return vos;
    }

    public List<getRecommendVo> getParentChild(Map<String,Long> map){
        List<getRecommendVo> vos = new ArrayList<>();
        List<Item> lists = itemFeign.getParentChild(map.get("itemTypeId"),map.get("ageGroup"),map.get("limit"));
        lists.forEach(list->{
            getRecommendVo vo = new getRecommendVo();
            BeanUtils.copyProperties(list,vo);
            vo.setAreaId(list.getCinema().getAreaId());
            vo.setAreaName(list.getCinema().getAreaName());
            vo.setAddress(list.getCinema().getAddress());
            vo.setStartDate(list.getStartTime());
            vo.setEndDate(list.getEndTime());
            Image image = imageFeign.image(list.getId(),2L, 1L);
            if (image != null){
                vo.setImgUrl(image.getImgUrl()==""?"":image.getImgUrl());
            }
            vos.add(vo);
        });
        return vos;
    }

    public List<getCalendarDataVo> getCalendarData(Long itemTypeId){
        List<getCalendarDataVo> daVos = new ArrayList<>();
        List<Item> lists = itemFeign.getCalendarData(itemTypeId);
        lists.forEach(list->{
            getCalendarDataVo daVo = new getCalendarDataVo();
            List<getRecommendVo> xiaoVos = new ArrayList<>();
            getRecommendVo xiaoVo = new getRecommendVo();
            BeanUtils.copyProperties(list,xiaoVo);
            Image image = imageFeign.image(list.getId(),2L, 1L);
            if (image != null){
                xiaoVo.setImgUrl(image.getImgUrl()==""?"":image.getImgUrl());
            }
            xiaoVo.setAreaId(list.getCinema().getAreaId());
            xiaoVo.setAreaName(list.getCinema().getAreaName());
            xiaoVo.setAddress(list.getCinema().getAddress());
            xiaoVo.setStartDate(list.getStartTime());
            xiaoVo.setEndDate(list.getEndTime());
            xiaoVos.add(xiaoVo);
            daVo.setDay(list.getStartTime().toString().replace("-","").trim().substring(0,8));
            daVo.setItemList(xiaoVos);
            daVos.add(daVo);
        });
        return daVos;
    }

}
